<template>
  <div class="app-container">
    <el-row>
      <el-tabs v-model="searchType" type="card" @tab-click="handleClick">
        <el-tab-pane label="订单数据" name="1"></el-tab-pane>
        <el-tab-pane label="用户数据" name="2"></el-tab-pane>
        <el-tab-pane label="权益数据" name="3"></el-tab-pane>
      </el-tabs>
    </el-row>
    <operation-options @getSearchOptions="getSearchOptions"></operation-options>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <div>
      <order-list
        :loading.sync="loading"
        :queryParams.sync="queryParams"
        :total.sync="total"
        :dataPoolDTO="dataPoolDTO"
        :list.sync="list"
        v-if="searchType == 1"
      ></order-list>
      <user-list
        :loading.sync="loading"
        :queryParams.sync="queryParams"
        :total.sync="total"
        :dataPoolDTO="dataPoolDTO"
        :list.sync="list"
        v-if="searchType == 2"
      ></user-list>
      <prize-list
        :loading.sync="loading"
        :queryParams.sync="queryParams"
        :total.sync="total"
        :dataPoolDTO="dataPoolDTO"
        :list.sync="list"
        v-if="searchType == 3"
      ></prize-list>
    </div>
  </div>
</template>

<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  resetUserPwd,
  changeUserStatus,
  deptTreeSelect,
  showMallList,
} from "@/api/system/user";
import { searchInfo } from "@/api/operation/operationSearch";
import OperationOptions from "../../components/operation/search.vue";
import OrderList from "../../components/operation/orderList.vue";
import UserList from "../../components/operation/userList.vue";
import PrizeList from "../../components/operation/prizeList.vue";
export default {
  name: "Operation",
  data() {
    return {
      searchType: "1",
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      list: null,
      //是否展开订单搜索更多选项
      isOrderSearch: false,
      //是否展开用户搜索更多
      isUserSearch: false,
      //是否展开权益搜索更多
      isPrizeSearch: false,
      // 表单参数
      form: {},
      dataPoolDTO: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },

      // 表单校验
      rules: {},
    };
  },
  components: {
    OperationOptions,
    OrderList,
    UserList,
    PrizeList,
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    // this.getList();
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event);
      this.searchType = tab.name;
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      searchInfo({
        dataPoolDTO: {
          ...this.dataPoolDTO,
          ...this.queryParams,
          searchType: Number(this.searchType),
        },
      }).then((response) => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getSearchOptions(data) {
      console.log(data);
      this.dataPoolDTO = { ...data };
      this.getList();
    },
    handleOrderMore() {
      this.isOrderSearch = !this.isOrderSearch;
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal
        .confirm('确认要"' + text + '""' + row.userName + '"用户吗？')
        .then(function () {
          return changeUserStatus(row.userId, row.status);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleResetPwd":
          this.handleResetPwd(row);
          break;
        case "handleAuthRole":
          this.handleAuthRole(row);
          break;
        default:
          break;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getUser().then((response) => {
        this.postOptions = response.posts;
        this.roleOptions = response.roles;
        this.open = true;
        this.title = "添加用户";
        this.form.password = this.initPassword;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids;
      getUser(userId).then((response) => {
        this.form = response.data;
        this.postOptions = response.posts;
        this.roleOptions = response.roles;
        this.$set(this.form, "postIds", response.postIds);
        this.$set(this.form, "roleIds", response.roleIds);
        this.open = true;
        this.title = "修改用户";
        this.form.password = "";
      });
    },

    /** 分配角色操作 */
    handleAuthRole: function (row) {
      const userId = row.userId;
      this.$router.push("/system/user-auth/role/" + userId);
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.userId != undefined) {
            updateUser(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal
        .confirm('是否确认删除用户编号为"' + userIds + '"的数据项？')
        .then(function () {
          return delUser(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/user/export",
        {
          ...this.queryParams,
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
